/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package earthdawnnet.client;

import java.io.Serializable;
import javax.swing.JOptionPane;

/**
 *
 * @author Andreas Franek
 */
class ServerData implements Serializable
{

    private String serverAddress;
    private int serverPort;
    private static final String serverCFG = "server.cfg";
    private static final Persister<ServerData> dataPersister = new Persister<ServerData>();
    private static ServerData instance = null;

    public static ServerData getInstance()
    {
        if (instance == null) 
        {
            askUserForData();
        }
        return instance;
    }

    private ServerData()
    {
        serverAddress = "127.0.0.1";
        serverPort = 5614;
    }

    public String getServerAddress()
    {
        return serverAddress;
    }

    public void setServerAddress(String serverAddress)
    {
        this.serverAddress = serverAddress;
    }

    public int getServerPort()
    {
        return serverPort;
    }

    public void setServerPort(int serverPort)
    {
        this.serverPort = serverPort;
    }

    public static void askUserForData()
    {
        ServerData serverData = load();
        String address = JOptionPane.showInputDialog("Enter server address", serverData.getServerAddress());
        if (address != null) {
            serverData.setServerAddress(address);
        }
        String port = JOptionPane.showInputDialog("Enter server port", serverData.getServerPort());
        if (port != null) {
            serverData.setServerPort(Integer.valueOf(port));
        }
        instance = serverData;
    }

    public static ServerData load()
    {
        ServerData result = dataPersister.load(serverCFG);
        if(result == null) result = new ServerData();
        return result;
    }

    public static void save()
    {
        if(instance == null) return;
        dataPersister.save(instance, serverCFG);
    }
}
